package DataAccess;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import Domain.Account;
import Domain.Catalog;
import Domain.Contact_information;
import Exception.DadosException;

public class DAOContact_information extends DAOGenerico<Contact_information>
		implements IDAOContact_information {

	public DAOContact_information() {
		super();
		// TODO Auto-generated constructor stub
	}

	public List<Contact_information> consultarTodos() {

		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("Accenture");
		EntityManager entityManager = emf.createEntityManager();

		TypedQuery<Contact_information> tq = entityManager.createQuery(
				"SELECT aa FROM Contact_information aa",
				Contact_information.class);

		List<Contact_information> list = tq.getResultList();
		entityManager.close();
		emf.close();
		return list;

	}

	public Contact_information consultarPorIdNOVO(Integer id) {

		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("Accenture");
		EntityManager entityManager = emf.createEntityManager();

		Contact_information c = entityManager.find(Contact_information.class,
				id);

		entityManager.close();
		emf.close();
		return c;
	}
	
	 public final void remover(Contact_information objeto) throws DadosException {

			EntityManagerFactory emf = Persistence.createEntityManagerFactory("Accenture");
			entityManager = emf.createEntityManager();

			EntityTransaction tx = entityManager.getTransaction();
			try {
				tx.begin();

				Query query = entityManager
						.createQuery("DELETE FROM Contact_information c WHERE c= :p");
				int deletedCount = query.setParameter("p", objeto).executeUpdate();

				tx.commit();
			} catch (Exception e) {
				e.printStackTrace();
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new DadosException(DadosException.ERROR_DEL, e);
			}

			entityManager.close();
			emf.close();
		}

}
